package com.imooc.timer;

import com.imooc.timer.listener.ExpirationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * <p>
 * redis 远程监听配置
 * 监听过期消息
 * 延时消息队列
 * 需要开启redis订阅模式：redis_conf 打开 'notify-keyspace-events Ex'
 * </p>
 *
 * @author gzj
 * @since 2023/11/1
 */
@Configuration
public class AuctionTimer {
    @Bean
    RedisMessageListenerContainer listenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        return listenerContainer;
    }

    @Bean
    KeyExpirationEventMessageListener redisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        return new ExpirationListener(listenerContainer);
    }
}
